package dao;

import entity.Article;
import entity.PageEntity;
import entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class ArticleDao extends BaseDao {

    /**
     * 文章搜索+分页
     *
     * @param title
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<Article> findPage(String title, Integer startIndex, Integer pageSize) {
        return jdbcTemplate.query("select * from article where title like ? order by publish_date desc limit ?,?", new BeanPropertyRowMapper<>(Article.class), "%" + title + "%", startIndex, pageSize);
    }

    /**
     * 根据标题查询符合记录的条数
     *
     * @param title
     * @return
     */
    public Integer countByTitle(String title) {
        return jdbcTemplate.queryForObject("select count(1) from article where title like ?", Integer.class, "%" + title + "%");
    }

    /**
     * 发布文章
     *
     * @param article
     */
    public void save(Article article) {
        jdbcTemplate.update("insert into article values (null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getId()
        );

    }

    /**
     * 浏览量+1
     *
     * @param id
     */
    public void updateBrowseCount(Long id) {
        jdbcTemplate.update("update article set browse_count = browse_count + 1 where id = ?", id);

    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    public Article findById(Long id) {
        return jdbcTemplate.queryForObject("select * from article where id = ?", new BeanPropertyRowMapper<>(Article.class), id);

    }

    /**
     * 根据用户和文章查询是否有收藏关系
     *
     * @param userId
     * @param articleId
     * @return
     */
    public Boolean isFavorite(Long userId, Long articleId) {
        Integer count = jdbcTemplate.queryForObject("select count(1) from favorite where u_id =? and a_id =?", Integer.class, userId, articleId);
        return count > 0;

    }

    /**
     * 根据用户id和文章id查询用户记录
     *
     * @param userId
     * @param articleId
     * @return
     */
    public List<User> findFavoriteList(Long userId, Long articleId) {
        String sql = "select * from user where id in (select user_focus_id from userfocus where user_id=? and user_focus_id in(select u_id from favorite where a_id = ?))";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), userId, articleId);

    }

    /**
     * 文章的收藏数量
     *
     * @param id
     * @return
     */
    public Integer findFavoriteCount(Long id) {
        return jdbcTemplate.queryForObject("select count(1) from favorite where a_id = ?", Integer.class, id);

    }

    /**
     * 取消收藏
     *
     * @param userId
     * @param articleId
     */
    public void delFavorite(Long userId, Long articleId) {
        jdbcTemplate.update("delete from favorite where u_id=? and a_id = ?", userId, articleId);

    }

    /**
     * 收藏文章
     *
     * @param userId
     * @param articleId
     */
    public void addFavorite(Long userId, Long articleId) {
        jdbcTemplate.update("insert into favorite values(?,?)", userId, articleId);
    }

    /**
     * 我的收藏+搜索+分页
     *
     * @param startIndex
     * @param pageSize
     * @param title
     * @param userId
     * @return
     */
    public List<Article> findFavoritePage(Integer startIndex, Integer pageSize, String title, Long userId) {
        String sql = "select * from favorite f inner join article a on a.id = f.a_id where u_id=? and a.title like ? limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class), userId,"%" + title + "%",startIndex, pageSize);
    }

    /**
     * 查询指定用户收藏的文章数量
     *
     * @param userId
     * @return
     */
    public Integer findFavoriteCountByUserId(Long userId) {
        return jdbcTemplate.queryForObject("select count(1) from favorite where u_id = ?", Integer.class, userId);
    }
}
